<?php

namespace app\advertisement\logic;

use app\api\logic\BasicLogic;
use app\api\logic\LinXingOrderLogic;
use think\facade\Db;

class MaterialLogic extends BasicLogic
{

    /**
     * @desc   新增素材信息接口
     * @author luckyXiong
     * @date   2025-07-04
     */
    public function add_material($params){
           $expert_id        = $params['expert_id']??'';
           $expert_name      = $params['expert_name']??'';
           $spu              = $params['spu']??'';
           $creation_code    = $params['creation_code']??'';
           $shop_id          = $params['shop_id']??'';
           $admin_id         = $params['admin_id']??'';
           $material_url     = $params['material_url']??'';
           $material_type    = $params['material_type']??'';
           $platform_list    = $params['material_list']??'';
           $material_arr     = $spu_arr = $platform_arr = [];
           //数据组装;
           $material_arr     = [
               'expert_id'    =>$expert_id,
               'expert_name'  =>$expert_name,
               'creation_code'=>$creation_code,
               'shop_id'      =>$shop_id,
               'material_url' =>$material_url,
               'material_type'=>$material_type,
               'operate_id'   =>$admin_id,
               'update_time'  =>date("Y-m-d H:i:s", time()),
               'create_time'  =>date("Y-m-d H:i:s", time()),
           ];

           $material_id = Db::name('tik_source_material')->insertGetId($material_arr);


           if($spu){
               $spu = explode(',',$spu);
               foreach($spu as $v){
                   $product_type = (new LinXingOrderLogic())->get_product_type(['spu'=>$v]);
                       $temp = [
                           'material_id'  =>$material_id,
                           'spu'          =>$v,
                           'product_type' =>$product_type,
                           'update_time'  =>date("Y-m-d H:i:s", time()),
                           'create_time'  =>date("Y-m-d H:i:s", time()),
                       ];
                   $spu_arr[] = $temp;
               }
               Db::name('tik_source_spu')->insertAll($spu_arr);
           }

           if($platform_list){
               foreach($platform_list as $v){
                   $temp = [
                       'material_id'  =>$material_id,
                       'platform_id'  =>$v['platform_id']??'',
                       'platform_url' =>$v['platform_url']??'',
                       'update_time'  =>date("Y-m-d H:i:s", time()),
                       'create_time'  =>date("Y-m-d H:i:s", time()),
                   ];
                   $platform_arr[] = $temp;
               }
               Db::name('tik_source_platform')->insertAll($platform_arr);
           }

    }


    /**
     * @desc    获取素材信息列表接口
     * @author  luckyXiong
     * @date    2025-07-04
     */
    public function material_list($params=[]){
        $limit                = getIsset($params,'limit',10);
        $spu                  = getIsset($params,'spu');
        $shop_id              = getIsset($params,'shop_id');
        $product_type         = getIsset($params,'product_type');
        $expert_name          = getIsset($params,'expert_name');
        $platform_id          = getIsset($params,'platform_id');
        $material_type        = getIsset($params,'material_type');
        $created_time         = getIsset($params,'create_time');
        $where                = [];
        $order = [];
        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }

        if (!empty($created_time)) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($created_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($created_time[1]) + 3600*24-1)];
        }

        if($product_type){
            $where[] = ['product_type', '=',$product_type];
        }

        if($shop_id){
            $where[] = ['shop_id', '=', $shop_id];
        }

        if($expert_name){
            $where[] = ['expert_name', '=',$expert_name];
        }

        if($platform_id){
            $where[] = ['platform_id', '=',$platform_id];
        }

        if($material_type){
            $where[] = ['material_type', '=',$material_type];
        }

        $order['create_time'] = 'desc';

        $field = '*';
        $res = Db::name('tik_source_material')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];
        $arr  = [];
        $operater_ids    = array_column($list,'operate_id');
        //批量获取创建人;
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        foreach($list as $k=>$v){
            $spu_info                 = self::get_spu($v);
            $admin_users_info         = $admin_users_map[$v['operate_id']]??'';
            $list[$k]['shop_name']    = 'wubilly';
            $list[$k]['spu']          = $spu_info['spu']??'';
            $list[$k]['product_type'] = $spu_info['product_type']??'';
            $list[$k]['operater']     = $admin_users_info['username']??'';
        }

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }



    private function get_spu($info=[]){
          $material_id = $info['id']??'';
          $list        = Db::name('tik_source_spu')->where(['material_id'=>$material_id])->select()->toArray();
          $spu         = array_column($list,'spu');
          $product_type= array_column($list,'product_type');
          $spu         = array_values(array_unique($spu));
          $product_type= array_values(array_unique($product_type));
          $product_type_arr = [
              '1'=>'bra类型',
              '2'=>'shapewear类型',
              '3'=>'swim类型',
              '4'=>'panties类型',
              '5'=>'其他'
          ];
          $arr = [];
          foreach($product_type as $v){
              $arr[] = $product_type_arr[$v]??'';
          }

          return  [
              'spu'          => implode(',',$spu),
              'product_type' => implode(',',$arr),

          ];
    }




}